package com.wtwd.campus.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.dao.SysRoleMapper;
import com.wtwd.campus.dao.SysUserMapper;
import com.wtwd.campus.entity.SysRole;
import com.wtwd.campus.entity.SysRolePermission;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.model.vo.SysRoleVO;
import com.wtwd.campus.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zdl
 * @since 2021-06-17
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysUserMapper sysUserMapper;


    @Override
    public IPage<SysRoleVO> queryPage(Page<SysRoleVO> page, SysRole sysRole) {
        return sysRoleMapper.querylist(page, sysRole);
    }

    @Override
    public int save(SysRole sysRole) {
        return sysRoleMapper.insert(sysRole);
    }

    @Override
    public int updateById(SysRole sysRole) {
        return sysRoleMapper.updateById(sysRole);
    }

    /**
     * 1.如果角色已绑定账号,提示无法删除
     * 2.删除角色信息及角色权限
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public ResultVO deleteById(Integer id) {
        if (new LambdaQueryChainWrapper<>(sysUserMapper)
                .eq(SysUser::getRoleId, id)
                .count() > 0) {
           return ResultVO.error(RespCode.ROLE_EXIST_USER);
        }
        sysRoleMapper.deleteById(id);
        sysRoleMapper.deleteRolePermissionByRoleId(id);
        return ResultVO.ok();
    }

    @Transactional
    @Override
    public ResultVO createRole(SysRole sysRole, String permissions) {
        save(sysRole);
        return saveRolePermissionBatch(sysRole.getId(), permissions);
    }

    @Transactional
    @Override
    public ResultVO updateRole(SysRole sysRole, String permissions) {
        sysRole.setOrgId(null);
        sysRole.setOrgCode(null);
        updateById(sysRole);
        sysRoleMapper.deleteRolePermissionByRoleId(sysRole.getId());
        return saveRolePermissionBatch(sysRole.getId(), permissions);
    }

    @Override
    public ResultVO saveRolePermissionBatch(Integer roleId, String permissions) {
        if (StringUtils.isBlank(permissions) || roleId == null) {
            return ResultVO.error(RespCode.ROLE_PERMISSION_ISNULL);
        }
        String[] split = permissions.split(",");
        ArrayList<SysRolePermission> sysRolePermissions = new ArrayList<>();
        for (String s : split) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setPermissionId(Integer.valueOf(s));
            sysRolePermission.setRoleId(roleId);
            sysRolePermissions.add(sysRolePermission);
        }
        sysRoleMapper.saveRolePermissionBatch(sysRolePermissions);
        return ResultVO.ok();
    }

    @Override
    public List<Integer> getRolePermissionIdsByRoleId(Integer roleId) {
        return sysRoleMapper.getRolePermissionIdsByRoleId(roleId);
    }

    @Override
    public List<SysRole> getRoleListByOrgId(Integer orgId) {
        List<SysRole> list = new LambdaQueryChainWrapper<>(sysRoleMapper)
                .eq(SysRole::getOrgId, orgId)
                .list();
        return list;
    }

}
